<template>
  <el-dialog title="新增员工"
             :visible="showDialong"
             width="30%"
             @close="close">
    <!-- 表单 -->
    <el-form label-width="100px"
             ref="form"
             :model='form'
             :rules='rules'>
      <el-form-item label="姓名"
                    prop="username">
        <el-input placeholder="请输入姓名"
                  v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机"
                    prop="mobile">
        <el-input placeholder="请输入手机号"
                  v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="工号"
                    prop="workNumber">
        <el-input placeholder="请输入工号"
                  v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门"
                    prop="departmentName">
        <el-cascader v-model="form.departmentName"
                     :options="list"
                     :props="defaultProps"
                     @focus="getDepartmentList" />
      </el-form-item>
      <el-form-item label="聘用形式"
                    prop="formOfEmployment">
        <el-select placeholder="请选择"
                   v-model="form.formOfEmployment">
          <el-option v-for="item in hireType.slice(0,2)"
                     :key="item.id"
                     :label="item.value"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间"
                    prop="timeOfEntry">
        <el-date-picker placeholder="请选择入职时间"
                        v-model="form.timeOfEntry" />
      </el-form-item>
      <el-form-item label="转正时间"
                    prop="correctionTime">
        <el-date-picker placeholder="请选择转正时间"
                        v-model="form.correctionTime" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template #footer>
      <el-button size="small"
                 @click="close">取消</el-button>
      <el-button type="primary"
                 size="small"
                 @click="add">确定</el-button>
    </template>
  </el-dialog>

</template>

<script>

import Employess from '@/api/constant/employees' // 导入的相关常量
import { getDepartmentList } from '@/api/departments'
import { addEmployee } from '@/api/employess'
import { listToTree } from '@/utils' // 把列表数据转成树状数据
export default {
  props: {
    showDialong: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: ['blur', 'change'] },
          { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '用户姓名为2-4位', trigger: ['blur', 'change'] }],
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间' }
        ]
      },
      // 相关常量
      hireType: Employess.hireType,
      list: [],
      // 组件库 动态渲染
      defaultProps: {
        label: 'name',
        value: 'name',
        expandTrigger: 'hover'
      }
    }
  },
  methods: {
    // 关闭弹窗
    close() {
      this.$emit('update:showDialong', false)
      this.$refs.form.resetFields()  // 重置
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    },
    // focus 事件 获取最新数据
    async getDepartmentList() {
      const data = await getDepartmentList()
      this.list = listToTree(data.depts)
      // console.log(this.list)
    },
    // 新增员工
    async add() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      // 校验通过 发请求添加 用...展开的方法 后面的参数可以覆盖前面的参数 截取最后一位数组的第一位
      await addEmployee({
        ...this.form,
        departmentName: this.form.departmentName.slice(-1)[0]
      })
      this.$message.success('添加成功')
      // 关闭弹窗
      this.$emit('update:showDialong', false)
      // 重新渲染
      this.$emit('getEmployeeList')
    }
  }
}
</script>

<style>
</style>
